package com.wwdx.controller.admin;

import com.wwdx.controller.base.BaseController;
import com.wwdx.model.WxMenuTree;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

/**
 * @author liguoliang
 * @date 2017/11/12
 */
@Controller
@RequestMapping("/system/wwdx_wechat_menu")
public class WxMenuTreeController extends BaseController{

    /**
     * 列表
     */
    @RequestMapping("/list")
    public ModelAndView list(WxMenuTree model){
        ModelAndView mv = new ModelAndView();
        mv.addObject("listSize",wxMenuTreeService.selectMenuCount(model));
        mv.addObject("list",wxMenuTreeService.selectMenuList(model));
        return mv;
    }

    /**
     * 新增
     */
    @RequestMapping("/create")
    public ModelAndView create(Long id) {
        ModelAndView mav = new ModelAndView("/system/wwdx_wechat_menu/create2");
        if (id != null){
            mav.addObject("model", wxMenuTreeService.getById(id));
            return mav;
        }
        return mav;
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    public ModelAndView edit(Long id) {
        ModelAndView mav = new ModelAndView();
        mav.addObject("model", wxMenuTreeService.getById(id));
        return mav;
    }

    /**
     * 新增保存
     */
    @RequestMapping("/save")
    public String save(WxMenuTree model,RedirectAttributes attributes) {
        Long pid = model.getPid();
        if (pid == null){
            model.setPid(0L);
        }else {
            model.setPid(model.getId());
        }
        model.setId(null);
        wxMenuTreeService.save(model);
        attributes.addAttribute("id",model.getPid());
        return "redirect:/system/wwdx_wechat_menu/list";
    }

    /**
     * 修改保存
     */
    @RequestMapping("/update")
    public String update(WxMenuTree model,RedirectAttributes attributes) {
        wxMenuTreeService.update(model);
        attributes.addAttribute("id",model.getPid());
        return "redirect:/system/wwdx_wechat_menu/list";
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresRoles("super")
    public String delete(Long id , RedirectAttributes attributes) {
        WxMenuTree model = wxMenuTreeService.getById(id);
        wxMenuTreeService.delete(model);
        attributes.addAttribute("id",model.getPid());
        return "redirect:/system/wwdx_wechat_menu/list";
    }

    /**
     * 生成菜单
     */
    @RequestMapping("/generate")
    public String generate()throws WxErrorException {
        List<WxMenuTree> menuList = wxMenuTreeService.selectMenuList(WxMenuTree.builder().id(0L).build());
        WxMenu menu = new WxMenu();
        //一级菜单
        menuList.forEach(model->{
            WxMenuButton oneButton = this.execute(model);
            //二级菜单
            List<WxMenuTree> menuSubList = wxMenuTreeService.selectMenuList(model);
            menuSubList.forEach(model2->{
                WxMenuButton twoButton = this.execute(model2);
                oneButton.getSubButtons().add(twoButton);
            });
            menu.getButtons().add(oneButton);
        });
        miDuWxService.getMenuService().menuCreate(menu);
        testWxService.getMenuService().menuCreate(menu);
        return "redirect:/system/wwdx_wechat_menu/list";
    }

    private WxMenuButton execute(WxMenuTree model){
        WxMenuButton button = new WxMenuButton();
        switch (model.getType()){
            case WxConsts.MenuButtonType.CLICK:
                button.setType(WxConsts.MenuButtonType.CLICK);
                button.setKey(model.getMenuKey());
                break;
            case WxConsts.MenuButtonType.VIEW:
                button.setType(WxConsts.MenuButtonType.VIEW);
                button.setUrl(model.getUrl());
                break;
            default:
                break;
        }
        button.setName(model.getName());
        return button;
    }

    /**
     * 清除菜单
     */
    @RequestMapping("/clear")
    public String clear()throws WxErrorException {
        miDuWxService.getMenuService().menuDelete();
        testWxService.getMenuService().menuDelete();
        return "redirect:/system/wwdx_wechat_menu/list";
    }
}